﻿namespace Resharper.Abstractions.CodeDom.Expressions.BinaryExpressions.AdditiveExpressions
{
  using System;
  using JetBrains.Annotations;
  using JetBrains.ReSharper.Psi.CSharp.Tree;
  using Resharper.Abstractions.Actions;

  /// <summary>Defines the <see cref="AdditiveExpressionExtensions"/> class.</summary>
  public static class AdditiveExpressionExtensions
  {
    #region Public Methods

    /// <summary>Gets the selected literal expression.</summary>
    /// <param name="context">The context.</param>
    /// <returns>Returns the selected literal expression.</returns>
    [CanBeNull]
    public static AdditiveExpression GetContainingAdditiveExpression([NotNull] this ProviderContext context)
    {
      if (context == null)
      {
        throw new ArgumentNullException("context");
      }

      var node = context.InternalGetSelectedElement<IAdditiveExpression>();
      if (node == null)
      {
        return null;
      }

      IAdditiveExpression result;
      do
      {
        result = node;
        node = node.GetContainingNode<IAdditiveExpression>();
      }
      while (node != null);

      return new AdditiveExpression(result);
    }

    #endregion
  }
}